#include "StdAfx.h"

#include "../Framework/Graphics/Camera.h"
#include "../Framework/Graphics/TgcScene.h"

using namespace std;

class c3DApp : public cApplication
{
    cTgcScene *m_pTgcScene;
    cCamera *m_pCamera;
    
protected:
    
public:
    virtual void Update(float timeDelta);
    virtual void DoFrame(float timeDelta);
    virtual void SceneInit();

    virtual void SceneEnd() {
    }

    c3DApp() :
    cApplication() {
        m_title = "TGC Viewer c++";
        m_width = 1024;
        m_height = 768;
        m_red = 0;
        m_green = 0;
        m_blue = 0;
        m_alpha = 0;
        m_bpp = 0;
        m_snl = 0;
        m_mode = GLFW_WINDOW;
        m_bActive = true;
    }

    ~c3DApp() {
        delete this->m_pTgcScene;
        delete this->m_pCamera;
    }

};

cApplication* CreateApplication() {
    return new c3DApp();
}

void DestroyApplication(cApplication* pApp) {
    delete pApp;
}

void c3DApp::SceneInit() {    
    this->m_pCamera = new cCamera();
    
    // DEFINICION DE PATH A XML
    std::string FilePathXml = cApplication::GetApplication()->m_dAppMedia.c_str();
    //std::string FilePathXmlModel = FilePathXml + "Box-TgcScene.xml";
    std::string FilePathXmlModel = FilePathXml + "Deposito-TgcScene.xml";
    //std::string FilePathXmlModel = FilePathXml + "Olla-TgcScene.xml";
    //std::string FilePathXmlModel = FilePathXml + "Robot-TgcScene.xml";
    //std::string FilePathXmlModel = FilePathXml + "Sarcofago-TgcScene.xml";
    //std::string FilePathXmlModel = FilePathXml + "Teapot-TgcScene.xml";
    //std::string FilePathXmlModel = FilePathXml + "Triangle-TgcScene.xml";
    //std::string FilePathXmlModel = FilePathXml + "BoxTriangle-TgcScene.xml";
    
    // ***********   cargar MODELO MEDIANTE TGC-SCENE   ***********
    this->m_pTgcScene = new cTgcScene((char*) FilePathXmlModel.c_str());
}

void c3DApp::Update(float timeDelta) {
    int x;
    double ttime;
    //ttime = timeDelta;
    ttime = glfwGetTime();
    // Obtener la posición del mouse
    glfwGetMousePos(&x, NULL);
    
    // Se actualiza cada modelo
    int i = 0;
    for(; i < this->m_pTgcScene->models_count; i++){
        //this->m_pTgcScene->m_pModels[i]->RotateModelInX(0.05f);
        this->m_pTgcScene->m_pModels[i]->Update(timeDelta);
    }
    
    this->m_pCamera->update(timeDelta);
}

void c3DApp::DoFrame(float timeDelta) {
    if (!Graphics())
        return;
    
    Graphics()->Clear();
    
    //Draw de los modelos
    int i = 0;
    for(; i < this->m_pTgcScene->models_count; i++){
        this->m_pTgcScene->m_pModels[i]->Draw();
    }
    
    this->m_pCamera->render();
}